package com.example.convertstream;

import java.io.*;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;

public class ConvertStreamDemo3 {
    public static void main(String[] args) throws IOException {
        /**
         * 将GBK文件转换为UTF-8文件
         */
        //了解
        //创建转换输入流对象
        InputStreamReader isr = new InputStreamReader(new FileInputStream("src/com/example/file/k.txt"), "GBK");
        //创建转换输出流对象
        OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream("src/com/example/file/l.txt"), StandardCharsets.UTF_8);

        //读取数据并保存数据
        int read = isr.read();
        while (read != -1) {
            osw.write(read);
            read = isr.read();
        }

        //释放资源
        osw.close();
        isr.close();

        //掌握
        //创建字符输入流对象
        FileReader fr = new FileReader("src/com/example/file/k.txt", Charset.forName("GBK"));
        //创建字符输出流对象
        FileWriter fw = new FileWriter("src/com/example/file/n.txt", StandardCharsets.UTF_8);

        //读取数据并保存数据
        int r = fr.read();
        while (r != -1) {
            fw.write(r);
            r = fr.read();
        }
        fw.close();
        fr.close();
    }
}
